package com.openapi.protocol.decode;

import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.decode.subPackageHandle.deviceInput.DeviceInputResponseHandle;
import com.openapi.protocol.decode.subPackageHandle.driverContrastResult.DriverContrastResultHandle;
import exsun.jt808.data.distribute_data.jt808.AreaProperty;
import exsun.jt808.data.distribute_data.Qingdao.PolygonAreaInfo;
import exsun.jt808.data.distribute_data.Qingdao.SortRouteInfo;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.Qingdao.*;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.TimeUtil;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.List;


@Component
public class AnalyzeQingdao {
    private final static Logger logger = LoggerFactory.getLogger(AnalyzeQingdao.class);


    private static DriverContrastResultHandle driverContrastResultHandle;
    @Autowired
    public void setDriverContrastResultHandle(DriverContrastResultHandle driverContrastResultHandle){
        AnalyzeQingdao.driverContrastResultHandle = driverContrastResultHandle;
    }

    private static DeviceInputResponseHandle deviceInputResponseHandle;
    @Autowired
    public void setDeviceInputResponseHandle(DeviceInputResponseHandle deviceInputResponseHandle){
        AnalyzeQingdao.deviceInputResponseHandle = deviceInputResponseHandle;
    }


    public static List<JT808PushData> driverContrastResultDecoder(JT808PackageData packageData){
        return driverContrastResultHandle.decode(packageData);
    }

    public static List<JT808PushData> deviceInputResponseDecoder(JT808PackageData packageData){
        return deviceInputResponseHandle.decode(packageData);
    }

    /**
     * 管控开关查询的应答
     */
    public static Jt808EntityBase ControlSwitchSortResponse(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        if(body.length == 0)return null;
        ControlSwitchSortResponse response = new ControlSwitchSortResponse();
        response.setLiftSwitch(DecoderUtils.parseIntFromBytes(body,0,1));
        response.setSpeeding(DecoderUtils.parseIntFromBytes(body,1,1));
        response.setBeyondBoundsAlarm(DecoderUtils.parseIntFromBytes(body,2,1));
        response.setUploadAlarm(DecoderUtils.parseIntFromBytes(body,3,1));
        response.setUnlicensedAlarm(DecoderUtils.parseIntFromBytes(body,4,1));
        response.setDriverAlarm(DecoderUtils.parseIntFromBytes(body,5,1));
        response.setBreakAlarm(DecoderUtils.parseIntFromBytes(body,6,1));
        response.setKeep(DecoderUtils.parseIntFromBytes(body,7,1));
        return response;
    }

    /**
     * 管控开关状态的应答
     */
    public static Jt808EntityBase ControlSwitchResponse(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        if(body.length == 0)return null;
        ControlSwitchResponse response = new ControlSwitchResponse();
        response.setLiftSwitch(DecoderUtils.parseIntFromBytes(body,0,1));
        response.setSpeedSwitch(DecoderUtils.parseIntFromBytes(body,1,1));
        response.setLockCarSwitch(DecoderUtils.parseIntFromBytes(body,2,1));
        return response;
    }

    /**
     * 查询多边形区域的应答
     */
    @SneakyThrows
    public static Jt808EntityBase SortPolygonAreaResponse(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        SortPolygonAreaResponse data = new SortPolygonAreaResponse();
        ArrayList<PolygonAreaInfo> response = new ArrayList<PolygonAreaInfo>();
        int count = DecoderUtils.parseIntFromBytes(body,0,1);
        int Index = 1;
        for (int n = 0; n < count; n++){
            PolygonAreaInfo item = new PolygonAreaInfo();
            AreaProperty area = new AreaProperty();
            item.setSignId(DecoderUtils.parseIntFromBytes(body,Index,4));
            Index += 4;
            int areaType = DecoderUtils.parseIntFromBytes(body,Index,2);
            //0000 0000 0000 0001 时间限制是否有效
            area.setbTime(((areaType & 0x0001) >> 0) == 1);
            //0000 0000 0000 0010 速度限制是否有效
            area.setbSpeed(((areaType & 0x0002) >> 1) == 1);
            //0000 0000 0000 0100 进区域是否报警给驾驶员
            area.setbInToDriver(((areaType & 0x0004) >> 2) == 1);
            //0000 0000 0000 1000 进区域是否报警给平台
            area.setbInToPlat(((areaType & 0x0008) >> 3) == 1);
            //0000 0000 0001 0000 出区域是否报警给驾驶员
            area.setbOutToDriver(((areaType & 0x0010) >> 4) == 1);
            //0000 0000 0010 0000 出区域是否报警给平台
            area.setbOutToPlat(((areaType & 0x0020) >> 5) == 1);
            //0000 0000 0100 0000 纬度类型
            area.setLatType((areaType & 0x0040) >> 6);
            //0000 0000 1000 0000 经度类型
            area.setLongType((areaType & 0x0080) >> 7);
            //0000 0001 0000 0000 0：允许开门；1：禁止开门
            area.setAllowOpenDoor((areaType & 0x0100) >> 8);
            //0000 1110 0000 0000 区域类型:9,10,11位
            area.setAreaType((areaType & 0x0e00));
            //0100 0000 0000 0000 0：进区域开启通信模块；1：进区域关闭通信模块
            area.setOpenCommunicationModule((areaType & 0x4000) >> 14);
            //1000 0000 0000 0000 0：进区域不采集 GNSS 详细定位数据；1：进区域采集 GNSS 详细定位数据
            area.setCollectGNSSInfo((areaType & 0x8000) >> 15);
            Index += 2;
            item.setStartTime(TimeUtil.getDateByFormat(DecoderUtils.parseBcdStringFromBytes(body,Index,6),TimeUtil.dateFormatyyMMddHHmmss));
            Index += 6;
            item.setEndTime(TimeUtil.getDateByFormat(DecoderUtils.parseBcdStringFromBytes(body,Index,6),TimeUtil.dateFormatyyMMddHHmmss));
            Index += 6;
            item.setBusinessValue(DecoderUtils.parseIntFromBytes(body,Index,1));
            Index += 1;
            item.setPoint(DecoderUtils.parseIntFromBytes(body,Index,2));
            Index += 2;
            for (int i = 0 ; i < item.getPoint(); i++){
                item.setLng(DecoderUtils.parseIntFromBytes(body,Index,4) / 1000000);
                Index += 4;
                item.setLat(DecoderUtils.parseIntFromBytes(body,Index,4) / 1000000);
                Index += 4;
            }
            data.setItems(response);
        }

        return data;
    }


    /**
     * 查询线路的应答
     */
    public static Jt808EntityBase SortRouteResponse(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        SortRouteResponse response = new SortRouteResponse();
        ArrayList<SortRouteInfo> items = new ArrayList<SortRouteInfo>();
        int count = DecoderUtils.parseIntFromBytes(body,0,1);
        int Index = 1;
        for (int n = 0;n < count; n++){
            SortRouteInfo item = new SortRouteInfo();
            item.setSignId(DecoderUtils.parseIntFromBytes(body,1,4));
            Index +=4;
            item.setRouteWidth(DecoderUtils.parseIntFromBytes(body,Index,1));
            Index +=1;
            item.setBusinessValue(DecoderUtils.parseIntFromBytes(body,Index,1));
            Index +=1;
            item.setPoint(DecoderUtils.parseIntFromBytes(body,Index,2));
            Index +=2;
            for (int i = 0; i < item.getPoint(); i++ ){
                item.setLng(DecoderUtils.parseIntFromBytes(body,Index,4) / 1000000);
                Index +=4;
                item.setLat(DecoderUtils.parseIntFromBytes(body,Index,4) / 1000000);
                Index +=4;
            }
            items.add(item);
            response.setItems(items);
        }
        return response;
    }

    /**
     * 驾驶员图片下载应答
     */
    public static Jt808EntityBase driverPictureDownLoad(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        DriverPictureDownLoadResponse response = new DriverPictureDownLoadResponse();
        response.setOrgSerialNum(DecoderUtils.parseIntFromBytes(body,0,2));
        response.setResult(DecoderUtils.parseIntFromBytes(body,2,1));
        response.setDownLoadNum(DecoderUtils.parseIntFromBytes(body,3,1));
        response.setFileNum(DecoderUtils.parseIntFromBytes(body,4,1));
        int driverIdLength = DecoderUtils.parseIntFromBytes(body,5,1);
        response.setDriverId(DecoderUtils.parseStringFromBytes(body,6,driverIdLength));
        int facePictureIdLength = DecoderUtils.parseIntFromBytes(body,6+driverIdLength,1);
        response.setFacePictureId(DecoderUtils.parseBcdStringFromBytes(body,7+driverIdLength,facePictureIdLength));
        return response;
    }

    /**
     * 驾驶员图片信息应答
     */
    public static Jt808EntityBase driverPictureMsgResponse(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        DriverPictureMsgResponse response = new DriverPictureMsgResponse();
        ArrayList<FaceMessage> items = new ArrayList<FaceMessage>();
        int count = DecoderUtils.parseIntFromBytes(body,0,1);
        for (int i = 0; i < count; i++){
            int Index = 1;
            FaceMessage item = new FaceMessage();
            int driverIdLen = DecoderUtils.parseIntFromBytes(body,Index,1);
            Index += 1;
            item.setDriverId(DecoderUtils.parseStringFromBytes(body,Index,driverIdLen));
            Index += driverIdLen;
            int JobSeniorityCardLen = DecoderUtils.parseIntFromBytes(body,Index,1);
            Index += 1;
            item.setJobSeniorityCard(DecoderUtils.parseBcdStringFromBytes(body,Index,JobSeniorityCardLen));
            Index += JobSeniorityCardLen;
            int setDriverNameLen = DecoderUtils.parseIntFromBytes(body,Index,1);
            Index += 1;
            item.setDriverName(DecoderUtils.parseBcdStringFromBytes(body,Index,setDriverNameLen));
            Index += setDriverNameLen;
            int setFacePictureIdLen = DecoderUtils.parseIntFromBytes(body,Index,1);
            Index += 1;
            item.setFacePictureId(DecoderUtils.parseBcdStringFromBytes(body,Index,setFacePictureIdLen));
            items.add(item);
            response.setFaceInfoList(items);
        }
        return response;
    }

//    /**
//     * 驾驶员对比结果上报
//     * @param packageData
//     * @return
//     */
//    public static Jt808EntityBase driverContrastResultResponse(JT808PackageData packageData){
//        byte[] body = packageData.getBodyBuffers();
//        DriverContrastResultResponse response = new DriverContrastResultResponse();
//        int m = body[8];
//        int n = body[8];
//        response.setResult(DecoderUtils.parseIntFromBytes(body,0,1));
//        response.setContrastSimilarityValue(DecoderUtils.parseIntFromBytes(body,1,1));
//        response.setContrastSimilarity(DecoderUtils.parseIntFromBytes(body,2,2));
//        response.setContrastType(DecoderUtils.parseIntFromBytes(body,4,1));
//        response.setDriverIdLength(DecoderUtils.parseIntFromBytes(body,5,1));
//        response.setDriverId(DecoderUtils.parseStringFromBytes(body,6,m));
//        response.setFacePictureIdLength(DecoderUtils.parseIntFromBytes(body,6+m,1));
//        response.setFacePictureId(DecoderUtils.parseBcdStringFromBytes(body,7+m,n));
//        response.setPositionMsg(DecoderUtils.parseIntFromBytes(body,7+m+n,28));
//        response.setPictureFormat(DecoderUtils.parseIntFromBytes(body,35+m+n,1));
//        response.setPicturePackage(DecoderUtils.parseIntFromBytes(body,36+m+n,body.length-(36+m+n)));
//        return response;
//    }

//    /**
//     * 设备录入驾驶员信息上报
//     */
//    public static Jt808EntityBase deviceInputResponse(JT808PackageData packageData){
//        byte[] body = packageData.getBodyBuffers();
//        DeviceInputResponse response = new DeviceInputResponse();
//        int m = body[8];
//        int n = body[8];
//        int L = body[8];
//        int h = body[8];
//        response.setDriverIdLength(DecoderUtils.parseIntFromBytes(body,0,1));
//        response.setDriverId(DecoderUtils.parseStringFromBytes(body,1,m));
//        response.setJobSeniorityCardLength(DecoderUtils.parseIntFromBytes(body,1+m,1));
//        response.setJobSeniorityCard(DecoderUtils.parseBcdStringFromBytes(body,2+m,n));
//        response.setDriverNameLength(DecoderUtils.parseIntFromBytes(body,2+m+n,1));
//        response.setDriverName(DecoderUtils.parseStringFromBytes(body,3+m+n,L));
//        response.setFacePictureIdLength(DecoderUtils.parseIntFromBytes(body,3+m+n+L,1));
//        response.setFacePictureId(DecoderUtils.parseStringFromBytes(body,4+m+n+L,h));
//        response.setPictureFormat(DecoderUtils.parseIntFromBytes(body,4+m+n+L+h,1));
//        response.setPicturePackage(DecoderUtils.parseIntFromBytes(body,5+m+n+L+h,body.length-(5+m+n+L+h)));
//        return response;
//    }

    //升级完成应答
    public static Jt808EntityBase upgradeCompleteResponseDecoder(JT808PackageData packageData) {
        byte[] body = packageData.getBodyBuffers();
        UpgradeCompleteResponse data=new UpgradeCompleteResponse();
        data.setType(DecoderUtils.parseIntFromBytes(body, 0, 1));
        int vNumberlength=(DecoderUtils.parseIntFromBytes(body, 1, 1));
        data.setVNumber(DecoderUtils.parseStringFromBytes(body,2,vNumberlength));
        data.setTheUpgrade(DecoderUtils.parseIntFromBytes(body,2+vNumberlength,1));
        data.setSynchronizationTime(DecoderUtils.parseBcdStringFromBytes(body,3+vNumberlength,6));
        data.setUpgradeCompletionTime(DecoderUtils.parseBcdStringFromBytes(body,9+vNumberlength,6));
        return data;
    }
    //设备版本上报
    public static Jt808EntityBase deviceVersionReporting(JT808PackageData packageData){
        byte[] body=packageData.getBodyBuffers();
        DeviceVersionReporting deviceVersionReporting=new DeviceVersionReporting();
        deviceVersionReporting.setReportType(DecoderUtils.parseIntFromBytes(body,0,1));
        int versionNumberLength=(DecoderUtils.parseIntFromBytes(body,1,1));
        deviceVersionReporting.setVersionNumber(DecoderUtils.parseStringFromBytes(body,2,versionNumberLength));
        return deviceVersionReporting;
    }




}
